// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

import Utils from "../common/Util";
import Facebook from "../Facebook/Facebook";
import MsgSystem from "../MsgSystem/MsgSystem";
import MsgDefine from "../MsgSystem/MsgDefine";
import Gmbox from "../gmbox/Gmbox";
import EventUtils from "../gmbox/EventUtils";

const {ccclass, property} = cc._decorator;

@ccclass
export default class RevivalGame extends cc.Component {

    @property(cc.Button)
    closeNode: cc.Button = null;

    @property(cc.Button)
    revivalGame: cc.Button = null;


    onLoad() {
        this.closeNode.node.on("click", this.showTipDialog, this);
        this.revivalGame.node.on("click", this.startRevivalGame, this);
    }

    start () {

    }

    public showTipDialog() {
        if (Gmbox.RewardedVideoAd) {
            Gmbox.RewardedVideoAd.abandonShow();
        }
        Utils.GetPrefab("prefab/GameArchive.prefab", (prefab) => {
            if (!prefab) return;
            var gameoverLayerNode = cc.instantiate(prefab);
            if (gameoverLayerNode) {
                let parent = this.node.parent;
                this.node.removeFromParent();
                parent.addChild(gameoverLayerNode);
            }
        }, this);
    }

    public startRevivalGame() {
        let successCallback = () => {
            EventUtils.logRevivial(true, "RevivalGame");
            if (this.node.isValid) {
                this.node.removeFromParent();
            }
            MsgSystem.GetInstance().PostMsg(MsgDefine.revivialGame);
        }
        let failCallback = () => {
            EventUtils.logRevivial(false, "RevivalGame");
        }
        if (Gmbox.RewardedVideoAd) {
            Gmbox.RewardedVideoAd.showRewardedVideoAd(successCallback, failCallback);
        } else {
            Facebook.FBPropAd.showRewardedVideoAd(successCallback, failCallback);
        }
    }

    // update (dt) {}
}
